package com.atguigu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

//实现全局跨域问题的解决
@Configuration
public class CrosConfig {
    //项目启动之后创建一个类对象，相当于是一个过滤器
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许所有的网络请求 对域名进行解除限制
        corsConfiguration.addAllowedOrigin("*");
        //可以携带cookie
        corsConfiguration.setAllowCredentials(true);
        //允许所有的请求方法
        corsConfiguration.addAllowedMethod("*");
        //允许所有请求头中携带信息
        corsConfiguration.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource configSource=new UrlBasedCorsConfigurationSource();
        //对所有请求进行配置 再设置到configSource里面去 然后在放到CorsWebFilter里面去
        configSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(configSource);
    }
}
